/***2024-11-20 迷途小羔羊
 * 用于编写角色顶部的堆叠物体
*/
namespace ghost
{
    export class RoleEffect implements IResource{
        
        public disposed: boolean;
        public roleEffMgr:RoleEffectMgr;
        protected _owner:AniRole;
        public height:number;
        protected _effect:AniRole;
        protected _id:number;
        protected _param:IRoleEffectParam;
        protected _scene:SceneManager;
        protected _effectID:number;
        public _sortId:number;
        /**构建角色头上的垂直堆叠效果
         * @param id 唯一id         
         * @param owner 效果的拥有者
         * @param param 附加参数
        */
        constructor(id:number, owner:AniRole, param:IRoleEffectParam)
        {
            let s= this;
            s._id = id;
            s._owner = owner;            
            s.setParam(param);
        }
        public setParam(param:IRoleEffectParam):void
        {
            let s= this;
            s._param = param;
            s.height = param.height?param.height:0;
            s._scene = s._owner._scene;
            s.effectCreate();
            if(s.disposed)
                return;
            s._effectID = s._effect._id;            
            s._effect.roleEffectOwner = s;             
            s.setSortId(s._param.sortId);
        }
        public getDefaultSortId():number
        {
            return 0;
        }
        public setSortId(sortId:number):void
        {
            let s = this;
            s._sortId = sortId>=0?sortId:s.getDefaultSortId();
        }
        public isValid():boolean
        {
            return this._effect && this._effectID == this._effect._id;
        }
        public get id():number
        {
            return this._id;
        }
        public get effect():AniRole
        {
            return this._effect;
        }        
        protected effectCreate():void
        {
            let s = this;
            let objID:string;
            let sts:number,direction:number,layerType:number;            
            objID = s._param.objID;
			if (objID == null) {
				s.dispose();
				return;
			}
            sts = s._param.sts?s._param.sts:StsType.STAND;
            direction = s._param.direction?s._param.direction:map2D.DirectionType.UP;
            layerType = s._param.layerType?s._param.layerType:map2D.LayerType.ROLE_BACK;
			if (s._effect) {
				let cfg: any;
				if (s._effect.cfg.roleID != objID) {
					cfg = RoleConfig.getObjConfig(objID);
					s._effect.parseConfig(cfg.roleType, cfg);
					s._effect.resetSkin(cfg.skin);                    
				}
			}
			else
            {
                let absX:number,absY:number;
                absX = s._param.offsetX!=null?s._owner.absX + s._param.offsetX:s._owner.absX;
                absY = s._param.offsetY!=null?s._owner.absY + s._param.offsetY:s._owner.absY - s._owner._headJumpH;
                s._effect = s._scene.createNoDepthObj(s._owner, RoleType.NO_DEPTH_OBJ, objID, absX, absY, sts, direction, layerType);
                s._effect.skin.mouseThrough = true;
            }				
			if(s._owner.layerType == map2D.LayerType.DEF)
				s._effect.setLayerType(map2D.LayerType.DEF, s._owner.layer);
            if (!s._owner._visible)
				s._effect.setVisible(s._owner._visible); 
        }
        protected effectRemove():void
        {
            let s = this;
            s.dispose();
        }
            
        public dispose():void
        {
            let s= this;
            if(s.disposed)return;
            s.disposed = true;            
            if(s.isValid())
            {
                if(s._effect)             
                {
                    s._effect.skin.mouseThrough = false;
                    s._effect.del();
                }
            }            
            s._effect = s._owner = null;
        }
    }
}